import xlrd
from core.entity import TableData as t
import core.sql as sql

# 获取所有Sheet对象的list
def get_sheet(path):
    # 文件位置
    excel_path = path
    # 打开文件，获取excel文件的workbook（工作簿）对象
    excel = xlrd.open_workbook(excel_path, encoding_override="utf-8")
    # 返回所有Sheet对象的list
    return excel.sheets()  # Book(工作簿)对象方法

# 获取Sheet对象的list中的全部数据
def get_sheet_data(cdb):
    # 获取所有Sheet对象的list
    all_sheet = get_sheet(cdb.excelpath)
    # 循环遍历每个sheet对象
    for sheet in all_sheet:
        print("该Excel共有{0}个sheet,当前sheet名称为{1},该sheet共有{2}行,{3}列".format(len(all_sheet), sheet.name, sheet.nrows, sheet.ncols))
        index = 1
        for each_row in range(sheet.nrows):  # 循环打印每一行
            if index == 1:
                t.table = sheet.row_values(each_row)
            elif index == 2:
                t.fieldName = sheet.row_values(each_row)
            elif index == 3:
                t.field = sheet.row_values(each_row)
            elif index == 4:
                t.fieldType = sheet.row_values(each_row)
                sql.operateMYSQL(sql.build_sql(t),cdb)
                index = 1
                continue
            index += 1